<?php

namespace app\model;

use think\Model;
use think\model\concern\SoftDelete;

class Order extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_at';
    protected $defaultSoftDelete = null;

    /**
     * 关联的数据表。
     *
     * @var string
     */
    protected $table = 'order';
    /**
     * 自动时间戳数据类型
     *
     * @var string
     */
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'create_at';
    protected $updateTime = 'update_at';

    /**
     * 属性默认值。
     *
     * @var array<int, string>
     */
    protected $attribute = [
        'user_id' => 0,  // 用户
    ];
    /**
     * The attributes that are mass assignable.
     *
     * @var array<string>
     */
    protected $field = [
        'id', // ID
        'status', // 状态
        'activity_id', // 活动
        'coupon_id', // 优惠券
        'memo', // 备注
    ];
    // 查询范围
    public function scopeUser($query)
    {
        $query->where('user_id', request()->uid);
    }
    public function searchActivityIdAttr($query, $value, $data)
    {
        $value > 0 && $query->where('activity_id', $value);
    }
    public function searchUserIdAttr($query, $value, $data)
    {
        $value > 0 && $query->where('user_id', $value);
    }
    public function searchMobileAttr($query, $value, $data)
    {
        !empty($value) && $query->hasWhere('user', ['mobile' => $value]);
    }
    public function searchRealnameAttr($query, $value, $data)
    {
        !empty($value) && $query->where('realname', 'like', '%' . $value . '%');
    }
    public function searchStatusAttr($query, $value, $data)
    {
        $value > 0 && $query->where('status', $value);
    }
    /**
     * 活动
     *
     * @return void
     */
    public function activity() {
        return $this->belongsTo('Activity','activity_id')->bind(['activity_title' => 'title', 'activity_fee' => 'fee']);
    }
    /**
     * 用户
     *
     * @return void
     */
    public function user() {
        return $this->belongsTo('User','user_id')->bind(['user_name' => 'nickname', 'mobile']);
    }
    /**
     * 支付信息
     *
     * @return void
     */
    public function pay() {
        return $this->hasOne('Pay','info_id')->where('info_type', 1)->bind(['pay_at', 'amount', 'pay_status' => 'status']);
    }
    /**
     * 地址
     *
     * @return void
     */
    public function address() {
        return $this->hasOne('OrderAddress');
    }
    /**
     * 优惠券
     *
     * @return void
     */
    public function coupon() {
        return $this->hasOne('Coupon', 'coupon_id');
    }
    public static function onBeforeInsert($model){
        $model->status = 1;
        $model->user_id = request()->uid;
    }
    public static function onAfterInsert ($model) {
        if($model->address_id > 0){
            $orderAddress = new OrderAddress;
            $data = UserAddress::field(['name', 'mobile', 'dist_id', 'address'])->findOrEmpty($model->address_id)->toArray();
            $orderAddress->save(['order_id' => $model->id] + $data);
        }

        $pay = new Pay;
        $pay->save([
            'amount' => $model->activity->fee,
            'memo' => '活动费',
            'info_id' => $model->id,
            'info_type' => 1
        ]);
    }
    public static function onAfterUpdate($model){
        if($model->status != $model->getOriin('status')){
            UserCoupon::update(['status' => $model->status == 2 ? 2 : 1], ['user_id' => $model->user_id, 'coupon_id' => $model->coupon_id]);
        }
        if($model->status == 2 && $model->getOriin('status') != 2){
            UserActivity::create(['user_id' => $model->user_id, 'activity_id' => $model->info_id]);
        }
    }
}
